package com.tjtt.common.gps;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

public class GpsUtil {

	/**
	 * 将非百度地图坐标的坐标进行转化，进而将其运用到百度地图开发的用户
	 * @param jingdu
	 * @param weidu
	 * @return
	 */
	public static BLH ReDefineGps(String jingdu,String weidu){
		String coords = jingdu+","+weidu;
		BLH blh = new BLH();
		String strjson="";
		try {
			strjson = sendPost("","http://api.map.baidu.com/geoconv/v1/?coords="+coords+"&from=1&to=5&ak=9AlAeUKHQtS2WcsKTdhlKF1j");
			//strjson = sendPost("","http://api.map.baidu.com/geoconv/v1/?coords="+coords+"&from=1&to=5&ak=D2b4558ebed15e52558c6a766c35ee73");
			if(strjson.indexOf("result")==-1)
			{
				strjson = sendPost("","http://api.map.baidu.com/geoconv/v1/?coords="+coords+"&from=1&to=5&ak=D2b4558ebed15e52558c6a766c35ee73");
				int times=0;
				while(strjson.indexOf("result")==-1&&times<10)
				{
					strjson = sendPost("","http://api.map.baidu.com/geoconv/v1/?coords="+coords+"&from=1&to=5&ak=D2b4558ebed15e52558c6a766c35ee73");					
					times++;
				}
			}
			JSONObject json = JSONObject.fromObject(strjson);
			JSONArray resultArray = (JSONArray)json.get("result");
			if(resultArray.size()==0)
			{
				return blh;
			}
			JSONObject json1 = resultArray.getJSONObject(0);
			blh.L =  Double.parseDouble(json1.get("x").toString());
			blh.B =  Double.parseDouble(json1.get("y").toString());
		} catch (IOException e) {
			// TODO Auto-generated catch block
			
			System.out.println(e.getMessage());
			System.out.println(strjson);
			e.printStackTrace();
		}
		return blh;
	}
	
	public static String sendPost(String jsonStr, String path)throws IOException {
		byte[] data = jsonStr.getBytes();
		java.net.URL url = new java.net.URL(path);
		java.net.HttpURLConnection conn = (java.net.HttpURLConnection) url.openConnection();
		conn.setRequestMethod("POST");
		conn.setConnectTimeout(5 * 1000);
		// 设置连接超时时间为5秒 
		conn.setReadTimeout(20 * 1000);
		// 设置读取超时时间为20秒 
		// 使用 URL 连接进行输出，则将 DoOutput标志设置为 true
		conn.setDoOutput(true);
		conn.setRequestProperty("Content-Type", "text/xml;charset=UTF-8");
		//conn.setRequestProperty("Content-Encoding","gzip");
		conn.setRequestProperty("Content-Length", String.valueOf(data.length));
		OutputStream outStream = conn.getOutputStream();
		// 返回写入到此连接的输出流
		outStream.write(data);
		outStream.close();//关闭流
		String msg = "";// 保存调用http服务后的响应信息// 如果请求响应码是200，则表示成功
		if (conn.getResponseCode() == 200) 
		{
			// HTTP服务端返回的编码是UTF-8,故必须设置为UTF-8,保持编码统一,否则会出现中文乱码
			InputStream is=conn.getInputStream();
			BufferedReader in = new BufferedReader(new InputStreamReader((InputStream) conn.getInputStream(), "UTF-8"));
			msg = in.readLine();
			in.close();
			conn.disconnect();// 断开连接
			if(msg==null)
			{
				conn.disconnect();// 断开连接
				return sendPost(jsonStr,path);	
			}
			return msg;
		}
		else
		{
			conn.disconnect();// 断开连接
			return sendPost(jsonStr,path);	
		}
//		conn.disconnect();// 断开连接
//		return msg;
	}

//	public static String sendPost(String jsonStr, String path)throws IOException {
//		byte[] data = jsonStr.getBytes();
//		java.net.URL url = new java.net.URL(path);
//		java.net.HttpURLConnection conn = (java.net.HttpURLConnection) url.openConnection();
//		conn.setRequestMethod("POST");
//		conn.setConnectTimeout(5 * 1000);
//		// 设置连接超时时间为5秒 
//		conn.setReadTimeout(20 * 1000);
//		// 设置读取超时时间为20秒 
//		// 使用 URL 连接进行输出，则将 DoOutput标志设置为 true
//		conn.setDoOutput(true);
//		conn.setRequestProperty("Content-Type", "text/xml;charset=UTF-8");
//		//conn.setRequestProperty("Content-Encoding","gzip");
//		conn.setRequestProperty("Content-Length", String.valueOf(data.length));
//		OutputStream outStream = conn.getOutputStream();
//		// 返回写入到此连接的输出流
//		outStream.write(data);
//		outStream.close();//关闭流
//		String msg = "";// 保存调用http服务后的响应信息// 如果请求响应码是200，则表示成功
//		if (conn.getResponseCode() == 200) 
//		{
//			// HTTP服务端返回的编码是UTF-8,故必须设置为UTF-8,保持编码统一,否则会出现中文乱码
//			BufferedReader in = new BufferedReader(new InputStreamReader((InputStream) conn.getInputStream(), "UTF-8"));
//			msg = in.readLine();
//			in.close();		
//		}
//		conn.disconnect();// 断开连接
//		return msg;
//	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		BLH blh = GpsUtil.ReDefineGps("114.21892734521", "29.575429778924");
		System.out.println(blh.B+","+blh.L);
	}
	
	
}
